package com.jflm.controller.api;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jflm.base.BaseAction;
import com.jflm.domain.DataInterfaceManagement;
import com.jflm.domain.InterfaceDetails;
import com.jflm.domain.InterfaceRelation;
import com.jflm.service.IInterfaceDetailsService;
import com.jflm.service.IInterfaceRelationService;

@RestController
@RequestMapping("/api/interfaceDetails")
public class InterfaceDetailsAction extends BaseAction{
	
	private static final Logger LOG = LoggerFactory.getLogger(InterfaceDetailsAction.class);
	
	@Autowired
	private IInterfaceDetailsService interfaceDetailsService;
	
	@Autowired
	private IInterfaceRelationService interfaceRelationService;
	
	/**
	 * ******************************************* 
	 * method name : findList
	 * description : 查询全部接口
	 * @author     : xusai
	 * @return 	   : List<InterfaceDetails>
	 * @param 	   : @throws Exception
	 * @see 	   : 
	 * *******************************************
	 * */
	@PostMapping(value = "/findList")
	public List<InterfaceDetails> findList(@RequestBody InterfaceDetails interfaceDetails) throws Exception {
		LOG.info("#findList() 请求参数：{}", gson.toJson(interfaceDetails));
		List<InterfaceDetails> reList = interfaceDetailsService.findList(interfaceDetails);
		LOG.info("#findList() 响应参数：{}", gson.toJson(interfaceDetails));
		return reList;
	}
	
	/**
	 * ******************************************* 
	 * method name : addPage
	 * description : 新增通道
	 * @author     : xusai
	 * @return 	   : List<InterfaceDetails>
	 * @param 	   : @throws Exception
	 * @see 	   : 
	 * *******************************************
	 * */
	@PostMapping(value="/addPage")
	public int add(@RequestBody InterfaceDetails interfaceDetails) throws Exception{
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		Date date = new Date();       
		Timestamp nowdate = new Timestamp(date.getTime());
		interfaceDetails.setId(uuid);
		interfaceDetails.setCreateTime(nowdate);
		interfaceDetails.setDel(0);
		interfaceDetails.setState("启用");
		int flag = interfaceDetailsService.add(interfaceDetails);
		InterfaceRelation interfaceRelation = new InterfaceRelation();
		String uuid2 = UUID.randomUUID().toString().replaceAll("-", "");
		interfaceRelation.setId(uuid2);
		interfaceRelation.setWayId(interfaceDetails.getWayId());
		interfaceRelation.setRuleId(uuid);
		interfaceRelation.setDel(0);
		interfaceRelation.setCreateTime(nowdate);
		int flag2 = interfaceRelationService.addRelation(interfaceRelation);
		return flag;
	}

	/**
	 * ******************************************* 
	 * method name : findOne
	 * description : 查询单条数据
	 * @author     : xusai
	 * @return 	   : List<InterfaceDetails>
	 * @param 	   : @throws Exception
	 * @see 	   : 
	 * *******************************************
	 * */
	@RequestMapping(value = "/findOne", method = RequestMethod.POST)
	public InterfaceDetails findOne(@RequestBody InterfaceDetails interfaceDetails) throws Exception {
		interfaceDetails= interfaceDetailsService.findOne(interfaceDetails.getId());
		LOG.info("#findOne() 响应参数：{}", gson.toJson(interfaceDetails));
		return interfaceDetails;
	}
	
	/**
	 * ******************************************* 
	 * method name : updatePage
	 * description : 修改接口
	 * @author     : xusai
	 * @return 	   : int
	 * @param 	   : @throws Exception
	 * @see 	   : 
	 * *******************************************
	 * */
	@PostMapping(value = "/updatePage")
	public int updatePage(@RequestBody InterfaceDetails interfaceDetails) throws Exception {
		int rul = interfaceDetailsService.update(interfaceDetails);
		return rul;
	}
	
	/**
	 * ******************************************* 
	 * method name : deletePage
	 * description : 禁用通道
	 * @author     : xusai
	 * @return 	   : int
	 * @param 	   : @throws Exception
	 * @see 	   : 
	 * *******************************************
	 * */
	@RequestMapping(value = "/deletePage", method = RequestMethod.POST)
	public int deletePage(@RequestBody InterfaceDetails interfaceDetails) throws Exception {
//		if(dataInterfaceManagement.getState().equals("启用")){
//			dataInterfaceManagement.setState("禁用");
//		}else{
//			dataInterfaceManagement.setState("启用");
//		}
		interfaceDetails.setState("禁用");
		int rul = interfaceDetailsService.delete(interfaceDetails);
		LOG.info("#deletePage() 响应参数：{}", gson.toJson(interfaceDetails));
		return rul;
	}
}
